package com.afei.java.ai.langchain4j.config;

import com.afei.java.ai.langchain4j.store.MongoChatMemoryStore;
import dev.langchain4j.data.document.Document;
import dev.langchain4j.data.document.loader.FileSystemDocumentLoader;
import dev.langchain4j.data.segment.TextSegment;
import dev.langchain4j.memory.chat.ChatMemoryProvider;
import dev.langchain4j.memory.chat.MessageWindowChatMemory;
import dev.langchain4j.rag.content.retriever.ContentRetriever;
import dev.langchain4j.rag.content.retriever.EmbeddingStoreContentRetriever;
import dev.langchain4j.store.embedding.EmbeddingStoreIngestor;
import dev.langchain4j.store.embedding.inmemory.InMemoryEmbeddingStore;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.Arrays;
import java.util.List;

/**
 * @Title: AFeiAgentConfig
 * @Author wangyf
 * @Package com.afei.java.ai.langchain4j.config
 * @Date 2025/4/24 16:45
 * @description:
 */

@Configuration
public class AFeiAgentConfig {

    @Autowired
    private MongoChatMemoryStore mongoChatMemoryStore;

    @Bean
    public ChatMemoryProvider chatMemoryProviderAFei() {
        return memoryId ->
                MessageWindowChatMemory.builder()
                        .id(memoryId)
                        .maxMessages(20)
                        .chatMemoryStore(mongoChatMemoryStore)
                        .build();

    }

    @Bean
    public ContentRetriever contentRetrieverAfei(){

        //Document document1 = FileSystemDocumentLoader.loadDocument("/Users/wangyongfei/Documents/天津测试对接信息.txt");
        Document document1 = FileSystemDocumentLoader.loadDocument("/data/project/agent/1234.txt");

        List<Document> documents = Arrays.asList(document1);

        //使用内存向量存储
        InMemoryEmbeddingStore<TextSegment> objectInMemoryEmbeddingStore = new InMemoryEmbeddingStore<>();

        //使用默认的文档分割器
        EmbeddingStoreIngestor.ingest(documents,objectInMemoryEmbeddingStore);

        //从嵌入存储里检索和查询内容相关的信息
        return EmbeddingStoreContentRetriever.from(objectInMemoryEmbeddingStore);

    }
}
